<?php
namespace Swoole\Thrift;
use Thrift\Protocol\TBinaryProtocol;
use Thrift\Protocol\TMultiplexedProtocol;
use Thrift\Transport\THttpClient;
use Thrift\Transport\TBufferedTransport;
use Thrift\Exception\TException;
use Swoole\Service\ThriftServiceClient;
use Common\FileUtil;
use Common\Log;
class ThriftHttpClient
{
//     private static $thriftServer = array(); 
    private static $config = null;
    private static $services = array();
    private static $addressList = array();
    private static $thriftServerAddressList = array();
    public static function init($config){
        self::$config = $config;
        
        if(count(self::$config['address']) > 0){
            $address = self::$config['address'];
            foreach($address as $add){
                $add = parse_url($add);
                self::$addressList[] = array('host'=>$add['host'], 'port'=>$add['port']);
            }
            self::$thriftServerAddressList = self::$addressList;
        }
        $genDir = self::$config['gen-php'];
        $genFiles = FileUtil::getPathFiles($genDir);
        if(count($genFiles) > 0){
            foreach($genFiles as $file){
                $service_class = substr($file, strlen($genDir),  strlen($file) - strlen($genDir) - 4);
                $service_class = \str_replace(DIRECTORY_SEPARATOR, '\\', $service_class);
                $info = pathinfo($file);
                $serviceName = $info['filename'];
                self::$services[$serviceName] = $service_class . 'Client';  // LoginServiceClient
            }
        }
    }
    public static function getService($serviceName){
        if(empty(self::$config) == true){
            exit("error, config can't be empty!");
        }
        if(empty(self::$services[$serviceName]) == true){
            exit("service {$serviceName} not exist");
        }
        $serviceClass = self::$services[$serviceName];
        $server = self::getAvailableServiceServer();
        Log::info("getAvailableServiceServer".json_encode($server));
        $trans = new THttpClient($server['host'], $server['port']);
        $transport = new TBufferedTransport($trans);
        $protocol = new TBinaryProtocol($transport);
        $thriftServiceProtocol = new TMultiplexedProtocol($protocol, $serviceName);
        $thriftServiceClient = new $serviceClass($thriftServiceProtocol);
        return $thriftServiceClient;

    }
    
    private static function getAvailableServiceServer(){
        $filaPath = BASE_PATH . '/data/thrift_client_server.txt';
        if(file_exists($filaPath) == true){
            $content = FileUtil::readThriftServer($filaPath);
            $servers = json_decode($content, true);
            self::$thriftServerAddressList = array_merge(self::$addressList, $servers);
        }
        
        $count = count(self::$thriftServerAddressList);
        if($count > 1){
            return self::$thriftServerAddressList[rand(0, $count - 1)];
        }
        else{
            return self::$thriftServerAddressList[0];
        }
    }
}

?>